package com.neo33.domain.strategy.model.entity;

import com.neo33.types.common.Constants;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author neo33
 * @description 策略规则实体
 * @create 2025-02-10 09:36
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class StrategyRuleEntity {

    // 抽奖策略id
    private Long strategyId;
    // 抽奖奖品id
    private Long awardId;
    // 抽奖规则类型 【1-策略规则、2-奖品规则】
    private Integer ruleType;
    // 抽奖规则类型 【rule_lock】
    private String ruleModel;
    // 抽奖规则比值
    private String ruleValue;
    // 抽奖规则描述
    private String ruleDesc;

    /**
     * 获取权重值
     * 数据案例； 4000:102,103,104,105 5000:102,103,104,105,106
     * @return {@link Map }<{@link String }, {@link List }<{@link Integer }>>
     */
    public Map<String, List<Long>> getRuleWeightValues() {
        if (!"rule_weight".equals(ruleModel)) {
            return null;
        }
        String[] ruleValueGroups = ruleValue.split(Constants.SPACE);
        Map<String, List<Long>> resultMap = new HashMap<>();
        for (String ruleValueGroup : ruleValueGroups) {
            // 检查输入是否为空
            if (ruleValueGroup == null || ruleValueGroup.isEmpty()) {
                return resultMap;
            }
            // 分割字符串以获取键和值
            String[] parts = ruleValueGroup.split(Constants.COLON);
            if (parts.length != 2) {
                throw new IllegalArgumentException("rule_weight rule_value invalid input format" + ruleValueGroup);
            }
            // 解析值
            String[] valueStrings = parts[1].split(Constants.SPLIT);
            List<Long> values = new ArrayList<>();
            for (String valueString : valueStrings) {
                values.add(Long.parseLong(valueString));
            }
            // 将键和值放入Map中
            resultMap.put(ruleValueGroup, values);
        }

        return resultMap;
    }
}
